package com.csit.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.csit.bean.Pager;
import com.csit.entity.DataDictionary;
import com.csit.service.DataDictionaryService;
import com.csit.util.JsonAndBeanUtil;
import com.csit.util.ListAndJsonUtil;
import com.csit.util.ResponseMsgUtil;
import com.csit.util.StringUtil;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Action("dict")
public class DataDictionaryAction extends BaseAction implements ModelDriven<DataDictionary> {

	
	/**   
	  * serialVersionUID:TODO
	  *   
	  * @since v1.0  
	 **/   
	
	private static final long serialVersionUID = -2664190664377168853L;
	
	private DataDictionary dataDictionary = new DataDictionary();
	
	@Resource
	private DataDictionaryService dictionaryService;
	
	public DataDictionary getModel() {
		return dataDictionary;
	}
	
	public void add(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(StringUtil.isNull(dataDictionary.getDataDictionaryType())){
			msgUtil.setMsg("类型不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(StringUtil.isNull(dataDictionary.getDataDictionaryName())){
			msgUtil.setMsg("名称不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		dictionaryService.saveEntity(dataDictionary);
		msgUtil.setSuccess(true);
		ajaxJson(msgUtil.toJSON());
	}
	
	public void update(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(StringUtil.isNull(dataDictionary.getDataDictionaryType())){
			msgUtil.setMsg("类型不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		if(StringUtil.isNull(dataDictionary.getDataDictionaryName())){
			msgUtil.setMsg("名称不能为空");
			ajaxJson(msgUtil.toJSON());
			return;
		}
		dictionaryService.updateEntity(dataDictionary);
		msgUtil.setSuccess(true);
		ajaxJson(msgUtil.toJSON());
	}
	
	public void select(){
		Pager pager = new Pager(page, rows);
		String jsonString = dictionaryService.queryDataDictionary(pager, dataDictionary);
		ajaxJson(jsonString);
	}
	
	public void get(){
		dataDictionary = dictionaryService.getEntity(dataDictionary.getDataDictionaryID());
		String jsonString = JsonAndBeanUtil.getJsonString(dataDictionary);
		ajaxJson(jsonString);
	}
	
	public void selectByType(){
		List<DataDictionary> list = dictionaryService.getList("dataDictionaryType", dataDictionary.getDataDictionaryType());
		String[] propertiesName = {"dataDictionaryID","dataDictionaryName"}; 
		String jsonString = ListAndJsonUtil.getJSONStringWithOutRows(list, propertiesName);
		ajaxJson(jsonString);
	}
}
